<?php

function list_konstanta(){
	$header = array(
		'No'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Uraian'=>array('style'=>'border-bottom:2px solid;width:35%'),  
		'Nilai'=>array('style'=>'border-bottom:2px solid;width:60%'),  
	);
	$query = "SELECT * FROM konfigurasi_konstanta ORDER BY kolom_id ASC";	
	$result = my_query($query);
	$i=0;
	$row=array();
	while($ey = my_fetch_array($result )){
		$i++;
		if($ey['form_type'] =='money'){
			$val='Rp. '.rp_format($ey['value']);
		}else{
			$val=$ey['value'];
		}		
		$row[] = array(
			'#'=>position_text_align($i,'center'),
			'uraian'=>  $ey['kolom_label'] ,
			'nilai'=>  $val.' '.$ey['satuan'] ,
		);
	}	
	$datas = table_rows($row); 
	return table_builder($header , $datas ,  15 ,false  );
}

function form_konstanta(){

	$header = array(
		'No'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Uraian'=>array('style'=>'border-bottom:2px solid;width:35%'),  
		'Nilai'=>array('style'=>'border-bottom:2px solid;width:60%'),  
	);
	$query = "SELECT * FROM konfigurasi_konstanta ORDER BY kolom_id ASC";	
	$result = my_query($query);
	$i=0;
	$row=array();
	while($ey = my_fetch_array($result )){
		$i++;
//'textfield','textarea','kode','money','persen'		
		if($ey['form_type'] =='persen')$size = 5;
		elseif($ey['form_type'] =='kode')$size = 10;
		elseif($ey['form_type'] =='money')$size = 15;
		else $size= 45;
		if($ey['form_type'] =='money'){
			$val=rp_format($ey['value']);
		}else{
			$val=$ey['value'];
		}
			
		$temp = array(
			'name'=>'konstanta_'.$ey['kolom_id'],
			'value'=>isset($_POST['konstanta_'.$ey['kolom_id']]) ? $_POST['konstanta_'.$ey['kolom_id']]: $val,
			'size'=>$size
		);
		if($ey['form_type'] =='textarea')
			$form_temp = form_textarea($temp).trim($ey['satuan']);
		elseif($ey['form_type'] =='money')
			$form_temp = 'Rp. '.form_money($temp).' ' .trim($ey['satuan']);
		else
			$form_temp = form_dynamic($temp).($ey['form_type'] =='persen' ?'%':'').trim($ey['satuan']);

		$row[] = array(
			'#'=>position_text_align($i,'center'),
			'uraian'=>  $ey['kolom_label'] ,
			'konstanta'=>  $form_temp ,
		);
	}	
	$datas = table_rows($row);
	return '<form method="POST" name="formDataSubmit">'.table_builder($header , $datas ,  5 , false ).'</form>';
}

function validasi_konstanta(){
	$errsubmit = false;
	$err = array();
	$query = "SELECT * FROM konfigurasi_konstanta ORDER BY kolom_id ASC";	
	$result = my_query($query);
	while( $row = my_fetch_array($result) ){
	
		if($row['form_type'] =='persen'){
			if(! is_numeric($_POST['konstanta_'.$row['kolom_id']])){
				$errsubmit = true;
				$err[] = "Nilai ".$row['kolom_label']." belum benar";
			}elseif( (int) $_POST['konstanta_'.$row['kolom_id']] > 100 ){
				$errsubmit = true;
				$err[] = "Nilai ".$row['kolom_label']." belum benar";
			}
		}
		elseif($row['form_type'] =='money'){
			if( set_text_as_number( $_POST['konstanta_'.$row['kolom_id']])==0){
				$errsubmit = true;
				$err[] = "Nilai ".$row['kolom_label']." belum benar";
			}
		}	
	
	}
	if( $errsubmit){
		return $err;
	}
	
	return $errsubmit;
}

function submit_konstanta(){
	$query = "SELECT * FROM konfigurasi_konstanta ORDER BY kolom_id ASC";
	$result = my_query($query);
	$datas = array(); 
	while($te = my_fetch_array($result) ){ 
		if($te['form_type'] =='money'){
			$val = set_text_as_number($_POST['konstanta_'.$te['kolom_id']]) ;
		}else{
			$val = $_POST['konstanta_'.$te['kolom_id']] ;
		}	
		$datas['value']=my_type_data_str($val );  	
		my_update_record( 'konfigurasi_konstanta', 'kolom_id',$te['kolom_id'],$datas);
	}
	return true;
}
